package com.ewhale.feitengguest.ui.user.task.adapter;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.dto.TaskCommentDto;
import com.simga.library.adapter.recyclerview.BaseViewHolder;
import com.simga.library.adapter.recyclerview.MutiRecyclerAdapter;
import com.simga.library.utils.CheckUtil;
import com.simga.library.utils.CircleImageView;
import com.simga.library.utils.DateUtil;
import com.simga.library.utils.glide.GlideUtil;
import com.simga.library.widget.BGButton;

import java.util.List;

import butterknife.BindView;

public class CommentAdapter extends MutiRecyclerAdapter<TaskCommentDto> {

    public static int USER_SIDE = 1001;
    public static int SHOP_SIDE = 1002;
    private int adapterType = USER_SIDE;

    public CommentAdapter(List<TaskCommentDto> data, int adapterType) {
        super(data, R.layout.item_comment);
        this.adapterType = adapterType;
    }


    @Override
    protected BaseViewHolder<TaskCommentDto> getViewHolder() {
        return new ViewHolder(view);
    }

    class ViewHolder extends BaseViewHolder<TaskCommentDto> {
        @BindView(R.id.iv_avatar)
        CircleImageView mIvAvatar;
        @BindView(R.id.tv_name)
        TextView mTvName;
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.tv_content)
        TextView mTvContent;
        @BindView(R.id.iv_content1)
        ImageView mIvContent1;
        @BindView(R.id.iv_content2)
        ImageView mIvContent2;
        @BindView(R.id.iv_content3)
        ImageView mIvContent3;
        @BindView(R.id.tv_replyContent)
        TextView tvReplyContent;
        @BindView(R.id.btn_reply)
        BGButton btnReply;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void build(TaskCommentDto object, int position) {
            GlideUtil.loadPicture(object.getHeadPhoto(), mIvAvatar, R.drawable.default_image);
            mTvContent.setText(object.getComment());
            mTvName.setText(object.getNickName());
            mTvTime.setText(DateUtil.parseToyyyymmddhhmm(object.getCreateTimeStamp() * 1000));
            String imgs[] = object.getImgUrls().split(",");
            if (CheckUtil.isNull(object.getImgUrls())) {
                mIvContent1.setVisibility(View.GONE);
                mIvContent2.setVisibility(View.GONE);
                mIvContent3.setVisibility(View.GONE);
            } else {
                if (imgs.length == 1) {
                    mIvContent1.setVisibility(View.VISIBLE);
                    mIvContent2.setVisibility(View.GONE);
                    mIvContent3.setVisibility(View.GONE);
                    GlideUtil.loadPicture(imgs[0], mIvContent1, R.drawable.default_image);
                } else if (imgs.length == 2) {
                    mIvContent1.setVisibility(View.VISIBLE);
                    mIvContent2.setVisibility(View.VISIBLE);
                    mIvContent3.setVisibility(View.GONE);
                    GlideUtil.loadPicture(imgs[0], mIvContent1, R.drawable.default_image);
                    GlideUtil.loadPicture(imgs[1], mIvContent2, R.drawable.default_image);
                } else if (imgs.length == 3) {
                    mIvContent1.setVisibility(View.VISIBLE);
                    mIvContent2.setVisibility(View.VISIBLE);
                    mIvContent3.setVisibility(View.VISIBLE);
                    GlideUtil.loadPicture(imgs[0], mIvContent1, R.drawable.default_image);
                    GlideUtil.loadPicture(imgs[1], mIvContent2, R.drawable.default_image);
                    GlideUtil.loadPicture(imgs[2], mIvContent3, R.drawable.default_image);
                }
            }
            if (!CheckUtil.isNull(object.getReplyContent())) {
                String content = "商家回复：" + object.getReplyContent();
                SpannableStringBuilder builder = new SpannableStringBuilder(content);
                ForegroundColorSpan blue = new ForegroundColorSpan(Color.parseColor("#349BFA"));
                builder.setSpan(blue, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvReplyContent.setText(builder);
                tvReplyContent.setVisibility(View.VISIBLE);
                btnReply.setVisibility(View.GONE);
            } else {
                if(adapterType == USER_SIDE){
                    btnReply.setVisibility(View.GONE);
                }else{
                    btnReply.setVisibility(View.VISIBLE);
                }
                tvReplyContent.setVisibility(View.GONE);
            }
            btnReply.setOnClickListener(v -> {
                if(listener!=null){
                    listener.onClick(position);
                }
            });
        }
    }
    public interface onClickReplyListener{
        void onClick(int position);
    }
    public onClickReplyListener listener;

    public void setListener(onClickReplyListener listener) {
        this.listener = listener;
    }
}
